package com.eastfair.venuebooking.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.echo.core.EchoService;
import com.eastfair.venuebooking.dto.VenueBookAuditDTO;
import com.eastfair.venuebooking.dto.VenueBookAuditPageQuery;
import com.eastfair.venuebooking.dto.VenueBookAuditSaveDTO;
import com.eastfair.venuebooking.dto.VenueBookAuditUpdateDTO;
import com.eastfair.venuebooking.entity.VenueBookAudit;
import com.eastfair.venuebooking.service.VenueBookAuditService;
import com.eastfair.venuebooking.vo.VenueBookAuditVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 场馆预定审核表
 * </p>
 *
 * @author dqq
 * @date 2023-02-22
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/venueBookAudit")
@Api(value = "VenueBookAudit", tags = "场馆预定审核表")
@PreAuth(replace = "venuebooking:venueBookAudit:")
public class VenueBookAuditController extends SuperController<VenueBookAuditService, Long, VenueBookAudit, VenueBookAuditPageQuery, VenueBookAuditSaveDTO, VenueBookAuditUpdateDTO> {

    @Value("${com.eastfair.projectId}")
    private Long projectId;

    @Resource
    private EchoService echoService;

    @Resource
    private VenueBookAuditService venueBookAuditService;

    @Override
    public void handlerResult(IPage<VenueBookAudit> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<VenueBookAudit> venueBookAuditList = list
                .stream()
                .map((map) -> VenueBookAudit.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(venueBookAuditList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<VenueBookAuditPageQuery> params) {
        VenueBookAuditPageQuery pageQuery = params.getModel();
    }

    /**
     * 查询场馆预定审核VO.
     *
     * @param pageParams the page params
     * @return the r
     */
    @PostMapping("/query/venueBookAudit")
    public R<Page<VenueBookAuditVO>> queryVenueBookAudit(@RequestBody PageParams<VenueBookAuditDTO> pageParams) {
        log.info("queryVenueBookAudit - 查询场馆预定审核VO, pageParams={}", pageParams);
        return R.successDef(venueBookAuditService.listVenueBookAuditVO(pageParams));
    }

    /**
     * id 查询详情.
     *
     * @param id the id
     * @return the by id
     */
    @GetMapping("/get/{id}")
    public R<VenueBookAuditVO> getById(@PathVariable("id") Long id) {
        log.info("getById - id 查询详情, id={}", id);
        return R.successDef(venueBookAuditService.getVenueBookAuditVOById(id));
    }

    /**
     * 修改场馆预定审核.
     *
     * @param venueBookAuditDTO the venue book audit dto
     * @return the r
     */
    @SysLog("修改场馆预定申请")
    @PostMapping("/update")
    public R<Boolean> updateVenueBookAudit(@RequestBody @Validated({VenueBookAuditDTO.Save.class, VenueBookAuditDTO.Update.class})
                                               VenueBookAuditDTO venueBookAuditDTO) {
        log.info("updateVenueBookAudit - 修改场馆预定审核, venueBookAuditDTO={}", venueBookAuditDTO);
        return R.successDef(venueBookAuditService.updateVenueBookAudit(venueBookAuditDTO));
    }

    /**
     * 更新审核结果.
     *
     * @param venueBookAuditDTO the venue book audit dto
     * @return the r
     */
    @SysLog("审核服务预定")
    @PostMapping("/audit")
    public R<Boolean> auditVenueBook(@RequestBody VenueBookAuditDTO venueBookAuditDTO) throws Exception {
        log.info("auditVenueBook - 更新审核结果, venueBookAuditDTO={}", venueBookAuditDTO);
        return R.successDef(venueBookAuditService.updateAuditStatus(venueBookAuditDTO));
    }

    /**
     * 外部展览会议系统提交场馆预定申请.
     *
     * @param venueBookAuditDTO the venue book audit dto
     * @return the r
     */
    @PostMapping("/submit/for/external/conference")
    public R<Boolean> submitForExternalExhibitionConference(@RequestBody VenueBookAuditDTO venueBookAuditDTO) {
        log.info("submitForExternalExhibitionConference - 外部展览会议系统提交场馆预定申请, venueBookAuditDTO={}", venueBookAuditDTO);
        ContextUtil.setProjectId(projectId);
        return R.successDef(venueBookAuditService.saveForConfluence(venueBookAuditDTO));
    }
}
